package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TMenu;
import com.example.springboot2.oa.entity.TPower;
import com.example.springboot2.oa.entity.TRole;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.ITMenuService;
import com.example.springboot2.oa.service.ITPowerService;
import com.example.springboot2.oa.service.ITRoleService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-role")
public class TRoleController extends BaseController {
    @Autowired
    private ITMenuService menuService;

    @Autowired
    private ITRoleService roleService;

    @Autowired
    private ITPowerService powerService;

    @GetMapping("listRole")
    public ResponseMessage listRole(String token, HttpServletRequest request) {
        ResponseMessage rsp = authentication(token, request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<TRole> data = roleService.list(queryWrapper);
        return MakeResponse.success("200", "角色列表", data);
    }

    @PostMapping("listAll")
    public ResponseMessage listAll(@RequestBody TRole role, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<TRole> data = roleService.list(queryWrapper);
        return MakeResponse.success("200", "角色列表", data);
    }

    @PostMapping("add")
    public ResponseMessage add(@RequestBody TRole role, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name", role.getRoleName());
        if (roleService.getOne(wrapper) != null) {
            return MakeResponse.success("500", "角色已经存在");
        }

        role.setRoleId(String.valueOf(UUID.randomUUID()));
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());

        if (!roleService.save(role)) {
            return MakeResponse.success("500", "角色添加失败");
        }

//        List<TMenu> menuList = menuService.list(
//                (new QueryWrapper<TMenu>())
//                        .orderByDesc("menu_id")
//        );
//
//        if (menuList != null) {
//            menuList.forEach(new Consumer<TMenu>() {
//                @Override
//                public void accept(TMenu tMenu) {
//                    TPower power = new TPower();
//                    power.setStatus(0);
//                    power.setRoleId(role.getRoleId());
//                    power.setMenuId(tMenu.getMenuId());
//                    power.setPowerId(String.valueOf(UUID.randomUUID()));
//                    powerService.save(power);
//                }
//            });
//        }

        return MakeResponse.success("200", "角色添加成功");
    }

    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TRole role, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        role.setUpdateTime(new Date());
        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", role.getRoleId());
        if (!roleService.update(role, wrapper)) {
            return MakeResponse.success("500", "角色更新失败");
        }

        return MakeResponse.success("200", "角色更新成功");
    }

    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TRole> roleList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<String> roleIdList = new ArrayList<>();
        roleList.forEach(new Consumer<TRole>() {
            @Override
            public void accept(TRole user) {
                roleIdList.add(user.getRoleId());
            }
        });

        for (int i = 0; i < roleList.size(); i++) {
            TRole role = roleList.get(i);
            QueryWrapper<TUser> wrapper = new QueryWrapper<>();
            wrapper.eq("role_id", role.getRoleId());
            List<TUser> powers = userService.list(wrapper);
            if (powers.size() > 0) {
                return MakeResponse.success("500",
                        "角色正在被使用 " + role.getRoleName());
            }
        }

        QueryWrapper<TRole> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.in("role_id", roleIdList);
        if (!roleService.remove(deleteWrapper)) {
            return MakeResponse.success("500", "角色删除失败");
        }

        return MakeResponse.success("200", "角色删除成功");
    }
}
